package netty.tcp.tasker;

import io.netty.util.HashedWheelTimer;
import org.apache.lucene.util.NamedThreadFactory;

import java.util.concurrent.TimeUnit;

public class TaskerManager {

    private final static TaskerManager taskerManager = new TaskerManager();

    private final HashedWheelTimer wheelTimer;

    private TaskerManager() {
        NamedThreadFactory namedThreadFactory = new NamedThreadFactory("TW");
        wheelTimer = new HashedWheelTimer(
                namedThreadFactory,
                1, TimeUnit.MICROSECONDS,
                100, false, 1000000);

        wheelTimer.start();
    }

    public static TaskerManager instance() {
        return taskerManager;
    }

    public void trigger(TaskerMessageSender taskerMessageSender) {
        wheelTimer.newTimeout(
                taskerMessageSender,
                taskerMessageSender.getSession().getDelay(),
                TimeUnit.MICROSECONDS);
    }
}
